package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeTip;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketUserData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketTempPoint;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class NewMonopolyMarketUserDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketUserData> {


    public static String getNewMonopolyMarketUserDataSql = "select * from `t_u_new_monopoly_market_user_data` where `userId`=?";
    public NewMonopolyMarketUserData getNewMonopolyMarketUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateNewMonopolyMarketUserDataSql = "update `t_u_new_monopoly_market_user_data` set weekId=?,systemGroupId=?,guanYin=?,lastGuanYinRecTime=?,officeCurPos=?,officeLevel=?,officeNo=?," +
            "officeSeatTime=?,officeRobTimes=?,teaLastNum=?,teaLastGetTime=?,dailyOfficeResetTime=?,dailyResGrabResetTime=?,hasTip=?,enterOfficeReportLastTime=?,enterResGrabReportLastTime=?,officeTip=?," +
            "searchPointMap=?,waitDispatchPoint=?,activeTime=?,getServerReward=? where userId=?";
    @Override
    protected boolean updateData(NewMonopolyMarketUserData newMonopolyMarketUserData) {
        newMonopolyMarketUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getSystemGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getGuanYin()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketUserData.getLastGuanYinRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeCurPos()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeNo()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeRobTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getTeaLastNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getTeaLastGetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getDailyOfficeResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getDailyResGrabResetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketUserData.isHasTip()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getEnterOfficeReportLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getEnterResGrabReportLastTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketUserData.getOfficeTip() == null ? "" : JSON.toJSONString(newMonopolyMarketUserData.getOfficeTip())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketUserData.getSearchPointMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketUserData.getWaitDispatchPoint() == null ? "" : JSON.toJSONString(newMonopolyMarketUserData.getWaitDispatchPoint())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getActiveTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketUserData.isGetServerReward()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketUserData.getUserId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketUserDataSql = "insert into `t_u_new_monopoly_market_user_data`(userId,weekId,systemGroupId,guanYin,lastGuanYinRecTime,officeCurPos,officeLevel,officeNo," +
            "officeSeatTime,officeRobTimes,teaLastNum,teaLastGetTime,dailyOfficeResetTime,dailyResGrabResetTime,hasTip,enterOfficeReportLastTime,enterResGrabReportLastTime,officeTip," +
            "searchPointMap,waitDispatchPoint,activeTime,getServerReward) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketUserData newMonopolyMarketUserData) {
        newMonopolyMarketUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getSystemGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getGuanYin()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketUserData.getLastGuanYinRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeCurPos()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeNo()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeSeatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getOfficeRobTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getTeaLastNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getTeaLastGetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getDailyOfficeResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getDailyResGrabResetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketUserData.isHasTip()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getEnterOfficeReportLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getEnterResGrabReportLastTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketUserData.getOfficeTip() == null ? "" : JSON.toJSONString(newMonopolyMarketUserData.getOfficeTip())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketUserData.getSearchPointMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketUserData.getWaitDispatchPoint() == null ? "" :JSON.toJSONString(newMonopolyMarketUserData.getWaitDispatchPoint())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketUserData.getActiveTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketUserData.isGetServerReward()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketUserData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketUserData newMonopolyMarketUserData = new NewMonopolyMarketUserData();
        newMonopolyMarketUserData.setUserId(rs.getLong("userId"));
        newMonopolyMarketUserData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketUserData.setSystemGroupId(rs.getInt("systemGroupId"));
        newMonopolyMarketUserData.setGuanYin(rs.getInt("guanYin"));
        newMonopolyMarketUserData.setLastGuanYinRecTime(rs.getLong("lastGuanYinRecTime"));
        newMonopolyMarketUserData.setOfficeCurPos(rs.getInt("officeCurPos"));
        newMonopolyMarketUserData.setOfficeLevel(rs.getInt("officeLevel"));
        newMonopolyMarketUserData.setOfficeNo(rs.getInt("officeNo"));
        newMonopolyMarketUserData.setOfficeSeatTime(rs.getInt("officeSeatTime"));
        newMonopolyMarketUserData.setOfficeRobTimes(rs.getInt("officeRobTimes"));
        newMonopolyMarketUserData.setTeaLastNum(rs.getInt("teaLastNum"));
        newMonopolyMarketUserData.setTeaLastGetTime(rs.getInt("teaLastGetTime"));
        newMonopolyMarketUserData.setDailyOfficeResetTime(rs.getInt("dailyOfficeResetTime"));
        newMonopolyMarketUserData.setDailyResGrabResetTime(rs.getInt("dailyResGrabResetTime"));
        newMonopolyMarketUserData.setHasTip(rs.getBoolean("hasTip"));
        newMonopolyMarketUserData.setEnterOfficeReportLastTime(rs.getInt("enterOfficeReportLastTime"));
        newMonopolyMarketUserData.setEnterResGrabReportLastTime(rs.getInt("enterResGrabReportLastTime"));
        newMonopolyMarketUserData.setActiveTime(rs.getInt("activeTime"));
        String officeTip = rs.getString("officeTip");
        if(!StringUtils.isNullOrEmpty(officeTip)) {
            newMonopolyMarketUserData.setOfficeTip(JSON.parseObject(officeTip, MonopolyMarketOfficeTip.class));
        }
        newMonopolyMarketUserData.setSearchPointMap(JSON.parseObject(rs.getString("searchPointMap"), new TypeReference<Map<Integer, NewMonopolyMarketTempPoint>>() {}));

        String waitDispatchPoint = rs.getString("waitDispatchPoint");
        if(!StringUtils.isNullOrEmpty(waitDispatchPoint)) {
            newMonopolyMarketUserData.setWaitDispatchPoint(JSON.parseObject(waitDispatchPoint, NewMonopolyMarketTempPoint.class));
        }
        newMonopolyMarketUserData.setGetServerReward(rs.getBoolean("getServerReward"));
        return newMonopolyMarketUserData;
    }
}
